﻿@page "/Article/{Text?}"
@using LuckyFish.Blog.Models
@using Microsoft.EntityFrameworkCore
@inject NavigationManager navigation
@inject MessageService MessageService
@inject IDbContextFactory<BlogContext> DbFactory

<PageTitle>@Model.Title</PageTitle>
@if (!string.IsNullOrEmpty(Model.ImageUrl))
{
    <ImageViewer FitMode="ObjectFitMode.Contain" class="img" Url="@Model.ImageUrl"/>
}
<Row Id="scroll" ColSpan="4" ItemsPerRow="ItemsPerRow.One">
    <h3>@Model.Title</h3>
    <p>@Model.Brief</p>
</Row>

<div class="row g-3">
    @foreach (var a in Model.Labels)
    {
        <div class="col-6 col-sm-4 col-md-3 col-lg-auto">
            <a href="@($"/Label/{a.LabelName}")">
                <Badge Color="Color.Primary">
                    @a.LabelName
                </Badge>
            </a>
        </div>
    }
</div>

<Divider/>

@if (!string.IsNullOrEmpty(Model.HtmlContext))
{
    <div style="height: 100%" class="mb-3">@((MarkupString)Model.HtmlContext)</div>
}

<Divider/>

<Footer Text="Bootstrap Blazor" Target="#scroll">
    <Row RowType="RowType.Inline" ItemsPerRow="ItemsPerRow.Twelve">
        <Button OnClickWithoutRender="OnExport" Text="PDF"></Button>
    </Row>
</Footer>


@code {

    [Parameter]
    public string? Text { get; set; }

    private ArticleModel Model { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        if (string.IsNullOrEmpty(Text))
        {
            await MessageService.Show(new MessageOption()
            {
                Content = "未找着当前文章，正在为您跳转到主页"
            });
            navigation.NavigateTo("/");
            return;
        }
        var context = await DbFactory.CreateDbContextAsync();
        var result = await context.Articles.Include(x => x.Labels).FirstOrDefaultAsync(x => x.UrlName == Text);
        if (result == null)
        {
            await MessageService.Show(new MessageOption()
            {
                Content = "未找着当前文章，正在为您跳转到主页"
            });
            navigation.NavigateTo("/");
            return;
        }
        Model = result;
    }

    private async Task OnExport()
    {
    // await using var stream = await PdfService.ExportStreamAsync(Model.HtmlContext);
    // await DownloadService.DownloadFromStreamAsync("test-stream.pdf", stream);
    // await ToastService.Success("Pdf Export", "Export pdf stream success.");
        await Task.CompletedTask;
    }

}

<style>
    .img{
        height: 250px;
    }
</style>